//
//  SEChatCountdownTimeView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import UIKit

class SEChatCountdownTimeView: UIView {

    @IBOutlet private weak var timeLabel: UILabel!
    
    var countDownEndHandler: (() -> Void)?
    
    /// 秒时间戳
    private var timeStamp: Int = 0
    
    private var timer: SETimer?
    
    deinit {
        SELog("chat count down time view -- dinit.")
        invalidate()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
    }
    
    func fireTimer(with viewModel: SEChatViewModel) {
        if viewModel.inquiryInfo.type == .online &&
            viewModel.inquiryInfo.dealStatus == .waitConfirm {
            let str = "等待医生接诊..."
            let attributes = [
                NSAttributedString.Key.foregroundColor: Asset.textDark.color,
                NSAttributedString.Key.font: UIFont.regular(14)
            ]
            let attrStr = NSMutableAttributedString(string: str, attributes: attributes)
            timeLabel.attributedText = attrStr
            return
        }
        
        timeStamp = Int(viewModel.inquiryInfo.leftTime / 1000)
        
        if viewModel.inquiryInfo.type == .online &&
            viewModel.inquiryInfo.dealStatus == .confirming &&
            timeStamp == 0 {
            let str = "剩余问诊时间"
            let attributes = [
                NSAttributedString.Key.foregroundColor: Asset.textDark.color,
                NSAttributedString.Key.font: UIFont.regular(14)
            ]
            let attrStr = NSMutableAttributedString(string: str, attributes: attributes)
            timeLabel.attributedText = attrStr
            return
        }
        
        timeLabel.attributedText = setTimeAttributedString()
        
        guard timeStamp > 0 else { return }
        
        if timer == nil {
            timer = SETimer.scheduledTimer(with: 1.0, repeats: true, block: { [unowned self] _ in
                self.countDownTime()
            })
        }
    }
    
    private func invalidate() {
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
    }
    
    private func countDownTime() {
        timeLabel.attributedText = setTimeAttributedString()
        
        if timeStamp == 0 {
            invalidate()
            countDownEndHandler?()
        } else {
            timeStamp -= 1
        }
    }
    
    private func setTimeAttributedString() -> NSMutableAttributedString {
        if timeStamp == 0 {
            let str = "已经结束"
            let attributes = [
                NSAttributedString.Key.foregroundColor: Asset.warn.color,
                NSAttributedString.Key.font: UIFont.regular(14)
            ]
            let attrStr = NSMutableAttributedString(string: str, attributes: attributes)
            return attrStr
        }
        
        let timeStr = Date.timeStampToString(timeStamp: timeStamp)
        let attrStr = NSMutableAttributedString(string: "剩余问诊时间 \(timeStr)")
        let frontAttributes = [
            NSAttributedString.Key.foregroundColor: Asset.textGray.color,
            NSAttributedString.Key.font: UIFont.regular(14)
        ]
        attrStr.setAttributes(frontAttributes, range: NSRange(location: 0, length: 6))
        
        let lastAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor(hex: "#FFAF24"),
            NSAttributedString.Key.font: UIFont.medium(14)
        ]
        attrStr.setAttributes(lastAttributes, range: NSRange(location: 7, length: attrStr.length - 7))
        
        return attrStr
    }
}
